package cn.chengpengper.shortlink.convert;

import cn.chengpengper.shortlink.constant.ShortLinkConstant;
import cn.chengpengper.shortlink.controller.request.AccountRegisterRequest;
import cn.chengpengper.shortlink.enums.AuthTypeEnum;
import cn.chengpengper.shortlink.model.AccountDO;
import cn.chengpengper.shortlink.util.CommonUtil;
import cn.chengpengper.shortlink.util.IdUtil;
import org.apache.commons.codec.digest.Md5Crypt;

import java.nio.charset.StandardCharsets;

/**
 * @author huchengpeng
 * @version v1.0.0
 * @date 2023/8/22 21:32
 * @description Account 相关对象转换器
 */
public class AccountConverter {

    public static AccountDO registerRequest2DO(AccountRegisterRequest registerRequest) {
        AccountDO accountDO = new AccountDO();

        accountDO.setHeadImg(registerRequest.getHeadImg());
        accountDO.setPhone(registerRequest.getPhone());
        accountDO.setMail(registerRequest.getMail());
        accountDO.setUsername(registerRequest.getUsername());
        accountDO.setGmtCreate(CommonUtil.now());
        accountDO.setGmtModified(CommonUtil.now());

        // 设置账户ID
        accountDO.setAccountNo(IdUtil.genSnowFlakeId());

        // 设置默认级别
        accountDO.setAuth(AuthTypeEnum.DEFAULT.name());

        // 设置Salt
        accountDO.setSecret(ShortLinkConstant.SALT_PREFIX + CommonUtil.getStringNumRandom(8));

        // 获取加密密码
        String cryptPwd = Md5Crypt.md5Crypt(registerRequest.getPwd().getBytes(StandardCharsets.UTF_8), accountDO.getSecret());
        accountDO.setPwd(cryptPwd);

        return accountDO;
    }
}
